package com.ruoyi.wx.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.Goodskinds;
import com.ruoyi.wx.service.IGoodskindsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品分类Controller
 *
 * @author hxie
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/wx/goodskinds")
public class GoodskindsController extends BaseController
{
    @Autowired
    private IGoodskindsService goodskindsService;

    /**
     * 查询商品分类列表
     */
//    @PreAuthorize("@ss.hasPermi('wx:goodskinds:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goodskinds goodskinds)
    {
        startPage();
        List<Goodskinds> list = goodskindsService.selectGoodskindsList(goodskinds);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('wx:goodskinds:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Goodskinds goodskinds)
    {
        List<Goodskinds> list = goodskindsService.selectGoodskindsList(goodskinds);
        ExcelUtil<Goodskinds> util = new ExcelUtil<Goodskinds>(Goodskinds.class);
        return util.exportExcel(list, "goodskinds");
    }

    /**
     * 获取商品分类详细信息
     */
//    @PreAuthorize("@ss.hasPermi('wx:goodskinds:query')")
    @GetMapping(value = "/{kindsId}")
    public AjaxResult getInfo(@PathVariable("kindsId") Long kindsId)
    {
        return AjaxResult.success(goodskindsService.selectGoodskindsById(kindsId));
    }


    /**
     * 新增商品分类
     */
//    @PreAuthorize("@ss.hasPermi('wx:goodskinds:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Goodskinds goodskinds)
    {
        if (UserConstants.NOT_UNIQUE.equals(goodskindsService.checkKindsNameUnique(goodskinds)))
        {
            return AjaxResult.error("新增类目名称'" + goodskinds.getKindsName() + "'失败，类目名称已存在");
        }
        goodskinds.setCreateBy(SecurityUtils.getUsername());
        return toAjax(goodskindsService.insertGoodskinds(goodskinds));
    }

    /**
     * 修改商品分类
     */
//    @PreAuthorize("@ss.hasPermi('wx:goodskinds:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Goodskinds goodskinds)
    {
        if (UserConstants.NOT_UNIQUE.equals(goodskindsService.checkKindsNameUnique(goodskinds)))
        {
            return AjaxResult.error("修改类目名称'" + goodskinds.getKindsName() + "'失败，类目名称已存在");
        }
        goodskinds.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(goodskindsService.updateGoodskinds(goodskinds));
    }

    /**
     * 删除商品分类
     */
//    @PreAuthorize("@ss.hasPermi('wx:goodskinds:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{kindsIds}")
    public AjaxResult remove(@PathVariable Long[] kindsIds)
    {
        return toAjax(goodskindsService.deleteGoodskindsByIds(kindsIds));
    }

    /**
     * 上传图标
     * @param file
     * @return
     * @throws IOException
     */
    @Log(title = "上传图片",businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload",produces = {"application/json;charset=UTF-8"})
    public Map<String ,String> picture(@RequestParam("file") MultipartFile file) throws IOException{
        logger.info("上传图标");
        Assert.notNull(file,"文件不能为空!");
        //获取当前工作目录
        String projectPath =System.getProperty("user.dir");

        String oldName =file.getOriginalFilename();
        //通过UUID生成唯一文件名
        String newName = UUID.randomUUID()+oldName.substring(oldName.lastIndexOf("."));
        String savePath =projectPath+"/ruoyi-wx/src/main/resources/static/image/goodskinds/"+newName;
        file.transferTo(new File(savePath));
        Map<String,String> map=new HashMap<>();
        map.put("name",newName);
        map.put("url","image/goodskinds/"+newName);
        return map;
    }
}
